﻿using System;
using System.ComponentModel;
using System.Windows.Controls;

namespace WPFTestSolution.Pages.Controls.RangeBases {
    
    /**
     * https://www.bilibili.com/video/BV1bV411A7Eq?p=44
     * 044-ScrollBar滚动条(: RangeBase: Control)
     * https://www.wpfsoft.com/2023/08/29/1530.html
     */
    public partial class _44_ScrollBar_Page : Page, INotifyPropertyChanged {
        public _44_ScrollBar_Page() {
            InitializeComponent();
            this.Title = "044-ScrollBar滚动条(: RangeBase: Control)";

            //将当前窗体作为ViewModel赋值给当前窗体的DataContext
            DataContext = this;
            Loaded += (s, e) => {
                //计算滚动条的最大值
                // Maximum = this.StackPanel1.ActualWidth - this.Grid.ActualWidth;
                Maximum = this.StackPanel1.ActualWidth - this.StackPanel0.ActualWidth;
                Console.WriteLine($"Maximum = {Maximum}");
            };
        }

        private double _maximum = 0;

        /// <summary>
        /// 滚动条的最大值
        /// </summary>
        public double Maximum{
            get => _maximum;
            set {
                _maximum = value;
                NotifyPropertyChanged("Maximum");
            }
        }

        private double _x = 0;

        /// <summary>
        /// 滚动条的当前值
        /// </summary>
        public double X{
            get => _x;
            set {
                _x = value;
                CanvasLeft = -_x;
                NotifyPropertyChanged("X");
            }
        }

        private double _canvasLeft = 0;

        /// <summary>
        /// 相对于Canvas控件Left边距
        /// </summary>
        public double CanvasLeft{
            get => _canvasLeft;
            set {
                _canvasLeft = value;
                NotifyPropertyChanged("CanvasLeft");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 属性通知方法
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void NotifyPropertyChanged(string propertyName) {
            if (this.PropertyChanged != null) {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            
           //一样的
           // this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
        }
    }
}